<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%system}}".
 *
 * @property int $id
 * @property string|null $name 平台名称
 * @property string|null $logo logo
 * @property string|null $contact_phone 联系方式
 * @property string|null $intr 简介
 * @property string|null $forward_title 转发标题
 * @property string|null $forward_cover 转发封面
 * @property string|null $share_title 分享标题
 * @property string|null $share_cover 分享封面
 * @property string|null $a_appid 小程序appid
 * @property string|null $a_secret 小程序secret
 * @property string|null $mch_id 微信商户号
 * @property string|null $pay_key 微信v2密钥
 * @property string|null $cert_path 微信cert证书
 * @property string|null $key_path 微信key证书
 * @property string|null $theme_color 主题色
 * @property float|null $start_price 配送起步价
 * @property int|null $start_num 配送起步件数
 * @property float|null $unit_price 超出配送单价
 * @property string|null $map_key web地图key
 * @property string|null $service_map_key 服务端地图key
 * @property string|null $withdraw_explain 提现说明
 * @property float|null $min_withdraw 最低提现金额
 * @property int|null $dis_review 分销申请审核
 * @property string|null $dis_agreement 分销协议
 * @property string|null $runn_agreement 跑腿协议
 * @property string|null $withdraw_way 提现方式
 * @property float|null $one_level 上级分销比例
 * @property float|null $second_level 上上级分销比例
 * @property float|null $top_level 顶级分销比例
 * @property string|null $dis_field 分销计算字段
 * @property int|null $order_cancel 订单关闭时间
 * @property int|null $order_receiv 订单自动确认
 * @property float|null $run_withdraw_fee 跑腿提现手续费
 * @property float|null $dis_withdraw_fee 分销提现手续费
 * @property float|null $run_percentage 配送抽成
 * @property float|null $ca_withdraw_fee 校区提现手续费
 * @property float|null $ca_percentage 校区抽成
 * @property float|null $max_run_num 最多接单数
 */
class System extends BaseModel
{
    public $timestamps = false;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%system}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['intr', 'withdraw_explain', 'dis_agreement', 'runn_agreement'], 'string'],
            [['start_price', 'unit_price', 'min_withdraw', 'one_level', 'second_level', 'top_level','run_withdraw_fee','dis_withdraw_fee','run_percentage','ca_withdraw_fee','ca_percentage'], 'number'],
            [['start_num', 'dis_review', 'order_cancel', 'order_receiv','max_run_num'], 'integer'],
            [['name', 'logo', 'contact_phone', 'forward_title', 'forward_cover', 'share_title', 'share_cover', 'a_appid', 'a_secret', 'mch_id', 'pay_key', 'cert_path', 'key_path', 'theme_color', 'map_key', 'service_map_key', 'withdraw_way', 'dis_field'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '平台名称',
            'logo' => 'logo',
            'contact_phone' => '联系方式',
            'intr' => '简介',
            'forward_title' => '转发标题',
            'forward_cover' => '转发封面',
            'share_title' => '分享标题',
            'share_cover' => '分享封面',
            'a_appid' => '小程序appid',
            'a_secret' => '小程序secret',
            'mch_id' => '微信商户号',
            'pay_key' => '微信v2密钥',
            'cert_path' => '微信cert证书',
            'key_path' => '微信key证书',
            'theme_color' => '主题色',
            'start_price' => '配送起步价',
            'start_num' => '配送起步件数',
            'unit_price' => '超出配送单价',
            'map_key' => 'web地图key',
            'service_map_key' => '服务端地图key',
            'withdraw_explain' => '提现说明',
            'min_withdraw' => '最低提现金额',
            'dis_review' => '分销申请审核',
            'dis_agreement' => '分销协议',
            'runn_agreement' => '跑腿协议',
            'withdraw_way' => '提现方式',
            'one_level' => '上级分销比例',
            'second_level' => '上上级分销比例',
            'top_level' => '顶级分销比例',
            'dis_field' => '分销计算字段',
            'order_cancel' => '订单关闭时间',
            'order_receiv' => '订单自动确认',
            'run_withdraw_fee' => '跑腿提现手续费',
            'dis_withdraw_fee' => '分销提现手续费',
            'run_percentage' => '配送抽成',
            'ca_withdraw_fee' => '校区提现手续费',
            'ca_percentage' => '校区抽成',
            'max_run_num' => '最多接单数',
        ];
    }

    /**
     * 获取系统设置
     * @return array|\yii\db\ActiveRecord|null
     */
    public static function getModel()
    {
        return self::find()->orderBy(['id'=>SORT_DESC])->one();
    }

    /**
     * 小程序配置
     * @return array
     */
    public static function applets()
    {
        $model = self::getModel();
        $config = [
            'app_id' => $model->a_appid??'',
            'secret' => $model->a_secret??'',
        ];
        return $config;
    }

    /**
     * 微信支付配置
     * @return array
     */
    public static function pay()
    {
        $system = self::getModel();
        $config = [
            'app_id'             => $system->a_appid??'',
            'mch_id'             => $system->mch_id??'',
            'key'                => $system->pay_key??'',
            'cert_path'          => $system->cert_path??'',
            'key_path'           => $system->key_path??'',
        ];
        if ($config['cert_path']){
            $config['cert_path'] = dirname(Yii::$app->BasePath).$config['cert_path'];
        }
        if ($config['key_path']){
            $config['key_path'] = dirname(Yii::$app->BasePath).$config['key_path'];
        }
        return $config;
    }
}
